import {
	Message,
	MessageBox,
	Loading
} from 'element-ui';

import $config from '@/admin.config.js'

let func = {};

func = new Proxy(func, {
	get: function(target, key) {
		return new FuncContext(key);
	}
});

function FuncContext(name) {
	this.funcname = name;
	this.response = {
		code: 200,
		datas: {},
		msg: ''
	}
}

FuncContext.prototype = {
	funcname: '', // 云函数名称
	response: {} // 统一响应格式
};

/**
 * 调用主空间云函数
 * action controller 函数路径
 * data controller 数据参数
 * */
FuncContext.prototype.maincall = function(action, data, loading = true) {
	if (loading) uni.showLoading({
		title: '请求中'
	}); 
	console.log(action)
	return uniCloud.callFunction({
		name: this.funcname,
		data: {
			action,
			data
		}
	}).then(res => {
		console.log(res)
		if ($config.debug) {
			console.log(this.funcname + '/' + action + ' call', res);
		}
		if (res.result) {
			this.response.code = res.result.code === 0 ? 200 : res.result.code;
			if (this.response.code != 200) {
				this.response.msg = res.result.msg || res.result.message;

				if (this.response.code == 'TOKEN_INVALID') {
					MessageBox.confirm('token失效，请重新登录！', '提示', {
						confirmButtonText: '确定',
						cancelButtonText: '取消',
						type: 'warning'
					}).then(() => {
						uni.removeStorageSync('wmzadmin_user');
						uni.removeStorageSync('uni_id_token');
						uni.removeStorageSync('uni_id_token_expired');

						uni.redirectTo({
							url: '/pages/login'
						});
					});
					return;
				}

				if (this.response.code == 'FORBIDDEN') {
					Message.error('此账号未授权');
				}

				if (this.response.code == 1) {
					Message.error(this.response.msg);
				}
			} else {
				this.response.msg = res.result.msg
			}
			this.response.datas = res.result.datas || res.result;
			return Promise.resolve(this.response);
		}

		return Promise.reject(res);
	}).catch(err => {
		if ($config.debug) {
			console.log(this.funcname + ' call catch', err);
			Message(JSON.stringify(err));
		}
		return Promise.reject(err);
	}).finally(res => {
		if (loading) uni.hideLoading();
	});
}

/**
 * 调用云函数
 * action controller 函数路径
 * data controller 数据参数
 * */
FuncContext.prototype.call = function(action, data, loading = true) {
	if (loading) uni.showLoading({
		title: '请求中'
	});
	let spaceId=uni.getStorageSync('wmzadmin_user_space_id')
	let cloud = cloud = uniCloud.init({
		provider: 'tencent',
		spaceId: spaceId
	});
	return cloud.callFunction({
		name: this.funcname,
		data: {
			action,
			data
		}
	}).then(res => {
		if ($config.debug) {
			console.log(this.funcname + '/' + action + ' call', res);
		}
		if (res.result) {
			this.response.code = res.result.code === 0 ? 200 : res.result.code;
			if (this.response.code != 200) {
				this.response.msg = res.result.msg || res.result.message;

				if (this.response.code == 'TOKEN_INVALID') {
					MessageBox.confirm('token失效，请重新登录！', '提示', {
						confirmButtonText: '确定',
						cancelButtonText: '取消',
						type: 'warning'
					}).then(() => {
						uni.removeStorageSync('wmzadmin_user');
						uni.removeStorageSync('uni_id_token');
						uni.removeStorageSync('uni_id_token_expired');

						uni.redirectTo({
							url: '/pages/login'
						});
					});
					return;
				}

				if (this.response.code == 'FORBIDDEN') {
					Message.error('此账号未授权');
				}

				if (this.response.code == 1) {
					Message.error(this.response.msg);
				}
			} else {
				this.response.msg = res.result.msg
			}
			this.response.datas = res.result.datas || res.result;
			return Promise.resolve(this.response);
		}

		return Promise.reject(res);
	}).catch(err => {
		if ($config.debug) {
			console.log(this.funcname + ' call catch', err);
			Message(JSON.stringify(err));
		}
		return Promise.reject(err);
	}).finally(res => {
		if (loading) uni.hideLoading();
	});
}

export default func;
